"""Setup the pydive application"""
import logging

import transaction
from tg import config

from pydive.config.environment import load_environment

log = logging.getLogger(__name__)

def setup_app(command, conf, vars):
    """Place any commands to setup pydive here"""
    load_environment(conf.global_conf, conf.local_conf)
    
    # Load the models
    from pydive import model
    print "Creating tables"
    macio = model.DBSession.query(model.User).filter(model.User.user_name=='macio').first()
    managers = model.DBSession.query(model.Group).filter(model.Group.group_name=='managers').first()
    try:
        macio.groups.index(managers)
    except:
        macio.groups.append(managers)
    """model.metadata.drop_all(bind=config['pylons.app_globals'].sa_engine)
    model.metadata.create_all(bind=config['pylons.app_globals'].sa_engine)
    
    u = model.User()
    u.user_name = u'macio'
    u.display_name = u'Maciej Kaniewski'
    u.email_address = u'mk@ediver.org'
    u.password = u'macio'
    d = model.Diver()
    d.firstname = u"Maciej"
    d.user.append(u)

    model.DBSession.add(u)
    
    u1 = model.User()
    u1.user_name = u'wrobell'
    u1.display_name = u'wrobell'
    u1.email_address = u'wrobell@ediver.org'
    u1.password = u'wrobell'
    
    d1 = model.Diver()
    d1.firstname = u"Wrobell"
    d1.user.append(u1)

    model.DBSession.add(u1)
    
    u2 = model.User()
    u2.user_name = u'cezar'
    u2.display_name = u'cezar'
    u2.email_address = u'cezar@ediver.org'
    u2.password = u'cezar'
    
    d2 = model.Diver()
    d2.firstname = u"Cezar"
    d2.user.append(u2)

    
    g1 = model.Group()
    g1.group_name = u'new_users'
    g1.display_name = u'New Users'
    

    model.DBSession.add(g1)
    
    g2 = model.Group()
    g2.group_name = u'users'
    g2.display_name = u'Users'
    

    model.DBSession.add(g2)

    g = model.Group()
    g.group_name = u'managers'
    g.display_name = u'Managers Group'
    
    
    g.users.append(u)
    g.users.append(u1)
    g.users.append(u2)

    model.DBSession.add(g)

    p = model.Permission()
    p.permission_name = u'manage'
    p.description = u'This permission give an administrative right to the bearer'
    p.groups.append(g)
    model.DBSession.add(p)
    
    p1 = model.Permission()
    p1.permission_name = u"profile"
    p1.description = u'Permission to view Profile of a diver'
    p1.groups.append(g)
    p1.groups.append(g2)
    model.DBSession.add(p1)
    
    p2 = model.Permission()
    p2.permission_name = u"logbook"
    p2.description = u'Permission to view edit log book'
    p2.groups.append(g)
    p2.groups.append(g2)
    model.DBSession.add(p2)
    
    p3 = model.Permission()
    p3.permission_name = u"new_profile"
    p3.description = u'Permission to create new Profile of a diver'
    p3.groups.append(g1)
    model.DBSession.add(p3)

    u1 = model.User()
    u1.user_name = u'editor'
    u1.display_name = u'Example editor'
    u1.email_address = u'editor@somedomain.com'
    u1.password = u'editpass'

    model.DBSession.add(u1)
    model.DBSession.flush()
    """
    transaction.commit()
    print "Successfully setup"
